<?php
if(!defined('BASEPATH'))
    exit ('No direct script access allowed');

class Path extends CI_Controller{
    private $page;
    public function __construct() {
        parent::__construct();
        $user_data = $this->session->all_userdata();
        if (isset($user_data["logged_in"]) && $user_data["logged_in"] == TRUE) {
            $this->page["header"] = $this->load->view("part/header_login", '', true);
        } else {
            $this->page["header"] = $this->load->view("part/header_no_login", '', true);
        }
    }
    /**
     * Action   :index
     * Purpose  :Return to home page (Home/index)
     * Date     :29/3/2012
     * Author   :Pham Truong
     */
    public function index(){

    }
    /**
     * Action   :browse
     * Purpose  :View all content in path which as #id
     * Date     :
     * Author   :
     */
    public function browse($id) {
        $page = $this->page;
        $this->load->model('path_model', 'path');
        $getId = $this->path->browseId($id);
        if ($getId == null)
            echo "Không tìm thấy lộ trình có id=" . $id;
        else {
            $data["path"] = $getId[0];
            $newestVersion = $this->path->getPathNewestVersion($id);
            $path_information =  $this->path->getInformation($newestVersion[0]["id"]);
            $data["information"] = $path_information[0];
            $data["information"]["comment_number"] = $this->path->commentNumber($id);
            $data["information"]["rating_number"] = $this->path->ratingNumber($id);
            $data["section"] = $this->path->getSection($newestVersion[0]["id"]);
            foreach ($data["section"] as $key => $secion) {
                $data["action"][$secion["id"]] = $this->path->getAction($secion["id"]);
            }
//            print("<pre>");
//            print_r($data["action"]);
//            print("</pre>");
            $data["contributor"] = $this->path->getContributor($id) ;
            $data["tag"] = $this->path->getTag($id);
            $page["content"] = $this->load->view('page/path/browse', $data, true);
            $this->load->view('layout', $page);
//            echo "<pre>";
//            print_r($data["information"]["comment_number"]);
//            echo "</pre>";
        }
    }
    /**
     * Action   :create
     * Purpose  :Create a new path
     * Date     :21/5/2012
     * Author   :Hoa Dang
     */
    public function create($step="1"){
        $page = $this->page;
        $user_data = $this->session->all_userdata();
        if (!isset($user_data["logged_in"]) || $user_data["logged_in"] == FALSE) {
            $page["message"]["error"] = "Bạn không có quyền tạo lộ trình mới.";
            $page["content"] = "";
        } else {
            $submitted = $this->input->post("mySubmit");
            if (!$submitted) {
                $this->load->model('home_model', 'home');
                $data['field_list'] = $this->home->getField();
                $page['message']['info'] = "Tạo lộ trình :: Bước 1";
                $page['content'] = $this->load->view('page/path/create_step1', $data, true);
            } else {
                $this->load->model('path_model', 'path');
                $data = array();
                if ($step == "1") {
                    $data["title"] = $this->input->post("title");
                    if ($data["title"] == null) {
                        show_error('error', 'Bạn chưa điền Tiêu đề');
                    }
                    $data["field_id"] = $this->input->post("field_id");
                    $data["permission"] = $this->input->post("permission");
                    $data["description"] = $this->input->post("description");
                    $data["member_id"] = 1; //Sau này sẽ lấy từ session
                    $data["tags"] = $this->input->post("tags");
                    $data["id"] = $this->path->createLearningPath($data);
                    $page['message']['success'] = "Đã tạo thành công lộ trình \"" . $data["title"] . "\". Vui lòng thêm dữ liệu cho lộ trình.";
                    $page['message']['info'] = "Tạo lộ trình :: Bước 2";
                    $page['content'] = $this->load->view('page/path/create_step2', $data, true);
                } else if ($step == "2") {
                    $pathID = $this->input->post("learning_path_id");
                    $pathVersionID = $this->input->post("path_version_id");
                    $data["level"] = $this->input->post("level");
                    $this->path->addLevelActionToLearningPath($pathVersionID, $data);
                    $page['message']['success'] = "Đã lưu lộ trình. <a href=" . base_url() . "path/browse/" . $pathID . ">Xem kết quả</a>";
                    $page['content'] = "";
                }
            }
        }
        $this->load->view('layout', $page);
    }
    /**
     * Action   :add_step
     * Purpose  :Add a step (has description) to path which has #id
     * Date     :
     * Author   :
     */
    public function add_step(){
        
    }
    /**
     * Action   :add_item_to_step
     * Purpose  :Add a content item to step which has #id
     *              Type:
     *                      - Read (article)
     *                      - View (video)
     *                      - Reference (link)
     * Date     :
     * Author   :
     */
    public function add_item_to_step(){
        
    }
    /**
     * Action   :remove_step
     * Purpose  :Remove a step which has #id
     * Date     :
     * Author   :
     */
    public function remove_step(){
        
    }
    /**
     * Action   :remove_item
     * Purpose  :Remove an item which has #id
     * Date     :
     * Author   :
     */
    public function remove_item(){
        
    }
    /**
     * Action   :edit_path_title
     * Purpose  :Edit title with #new_title for path which has #id
     * Date     :
     * Author   :
     */
    public function edit_path_title(){
        
    }
    /**
     * Action   :edit_path_desc
     * Purpose  :Edit description of path with #new_desc for path which has #id
     * Date     :
     * Author   :
     */
    public function edit_path_desc(){
        
    }
    /**
     * Action   :edit_step_title
     * Purpose  :Edit title with #new_title for step which has #step_id
     * Date     :
     * Author   :
     */
    public function edit_step_title(){
        
    }
    /**
     * Action   :edit_step_desc
     * Purpose  :Edit description of path with #new_desc for step which has #step_id
     * Date     :
     * Author   :
     */
    public function edit_step_desc(){
        
    }
    
//    public function test() {
//        $page = $this->page;
//        $submitted = $this->input->post("mySubmit");
//        if (!$submitted) {
//            $this->load->model('home_model', 'home');
//            $data['field_list'] = $this->home->getField();
//            $page['message']['info'] = "Tạo lộ trình :: Bước 1";
//            $page['content'] = $this->load->view('page/path/create_step1', $data, true);
//        } else {
//            
//            $this->load->model('path_model', 'path');
//            $data = array();
//            $data["title"] = $this->input->post("title");
//            if ($data["title"] == null) {
//                show_error('error', 'Bạn chưa điền Tiêu đề');
//            }
//            $data["field_id"] = $this->input->post("field_id");
//            $data["permission"] = $this->input->post("permission");
//            $data["description"] = $this->input->post("description");
//            $data["member_id"] = 1;//Sau này sẽ lấy từ session
//            $data["tags"] = $this->input->post("tags");
//            $page['content'] = "Đã thêm thành công lộ trình có ID=" . $this->path->createLearningPath($data);
//        }
//        $this->load->view('layout', $page);
//    }
    
//    public function test2() {
////        $level["title"] = "Test 1";
////        $level["description"] = "Test 1 Test 1 Test 1 ";
////        $action = array(
////            0 => array ("type"=>1, "title"=>"Bài viết 1", "link"=>"link1"),
////            1 => array ("type"=>2, "title"=>"Bài viết 2", "link"=>"link2")
////        );
////        $level["action"] = $action;
////        $data["level"][] = $level;
////        
////        $level["title"] = "Test 2";
////        $level["description"] = "Test 2 Test 2 Test 2 ";
////        $action = array(
////            0 => array ("type"=>3, "title"=>"Tham khảo 1", "link"=>"link1"),
////            1 => array ("type"=>3, "title"=>"Tham khảo 2", "link"=>"link2")
////        );
////        $level["action"] = $action;
////        $data["level"][] = $level;
//        $page = $this->page;
//        $this->load->model("path_model", "path");
//        $submitted = $this->input->post("mySubmit");
//        if (!$submitted) {
//            $page['content'] = $this->load->view('page/path/create_step2', '', true);
//            $this->load->view('layout', $page);
//        } else {
//            $data["level"] = $this->input->post("level");
//            $this->path->addLevelActionToLearningPath("3", $data);
//        }
//
//    }
    
    public function  get_tag_list() {
        $this->load->model("path_model", "path");
        $test_array = $this->path->getTagList();
        header('Content-Type: text/xml');
        
        echo '<?xml version="1.0" encoding="UTF-8" standalone="no"?>' . "\n";
        echo '<taglist style="MEDIUM">' . "\n";
        foreach ( $test_array as $tag) {
            echo "<tag>" . "\n";
            echo "<tagID>" . $tag["id"] . "</tagID>" . "\n";
            echo "<tagTitle>" . $tag["title"] . "</tagTitle>" . "\n";
            echo "</tag>" . "\n";
        }
        echo '</taglist>' . "\n";
    }
    
    public function get_article_list() {
        $this->load->model("path_model", "path");
        $test_array = $this->path->getArticleList();
        header('Content-Type: text/xml');
        
        echo '<?xml version="1.0" encoding="UTF-8" standalone="no"?>' . "\n";
        echo '<articlelist style="MEDIUM">' . "\n";
        foreach ( $test_array as $tag) {
            echo "<article>" . "\n";
            echo "<articleID>" . $tag["id"] . "</articleID>" . "\n";
            echo "<articleTitle>" . $tag["title"] . "</articleTitle>" . "\n";
            echo "<contributorID>" . $tag["contributor_id"] . "</contributorID>" . "\n";
            
            echo "</article>" . "\n";
        }
        echo '</articlelist>' . "\n";
    }
    /**
        * Description of path_history
        *
        * @author phamtruong
        */
    public function history($id) {
        $page = $this->page;
        $this->load->model("path_history_model");
        $data['bai']=  $this->path_history_model->get_title($id);
        $page['content'] = $this->load->view("page/path/history", $data, true);
        $this->load->view("layout", $page);
    }
}

/* End of file path.php */
/* Location: ./application/controllers/path.php */